package com.ai.AI_Interface.controller;

import com.ai.AI_Interface.mamage.AIManager;
import com.ai.common.model.BIRequest;
import com.ai.common.interfaces.BIInterFace;
import com.alibaba.nacos.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@Slf4j
public class BIInvokerAIController {
    @Resource
    private AIManager aiManager;

    private static Long ModelId = 1659171950288818178L;

    //这里因为是SDK调用所以暂时不需要用到任务队列执行
    @PostMapping("/ai/invokerBI")
    public HashMap<String, Object> invokerBI(@RequestBody BIRequest biRequest){
        HashMap<String, Object> result = new HashMap<>();
        log.info("调用传递参数：{}",biRequest);
        StringBuilder userInput = new StringBuilder();
        userInput.append("分析需求：").append("\n");
        // 拼接分析目标
        String userGoal = biRequest.getGoal();
        // 如果图表类型不为空
        if (StringUtils.isNotBlank(biRequest.getChartType())) {
            // 就将分析目标拼接上“请使用”+图表类型
            userGoal += "，请使用" + biRequest.getChartType();
        }
        userInput.append(userGoal).append("\n");
        userInput.append("原始数据：").append("\n");
        String csvData = biRequest.getCsvData().replace("#","\n");
        userInput.append(csvData).append("\n");
        String AIResult = aiManager.doChart(ModelId, userInput.toString());
        result.put("code","0000");
        result.put("msg","请求成功");
        result.put("data",AIResult);
        log.info("调用结果：{}",AIResult);
        return result;
    }
}
